﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace CSMF.Management.Page_Comment
{
    public partial class CommentList : System.Web.UI.Page
    {
        private CommentService manager = new CommentService();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["admin"] != null)
                {
                    BindDataForGridView(string.Empty);
                }
                else
                {
                    Response.Write("<script language='javascript'>parent.location.href='../login.aspx';</script>");
                }
            }
        }

        private void BindDataForGridView(string searchString)
        {
            List<CommentInfo> list = manager.GetCommentsListForAdmin(searchString);
            BindData(list);
        }

        private void BindData(List<CommentInfo> list)
        {
            this.AspNetPager1.RecordCount = list.Count;
            this.AspNetPager1.PageSize = 20;
            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = list;
            pds.AllowPaging = true;
            pds.PageSize = 20;
            pds.CurrentPageIndex = this.AspNetPager1.CurrentPageIndex - 1;
            gvUser.DataSource = pds;
            gvUser.DataBind();
        }

        protected void gvUser_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            LinkButton deleteButton = this.gvUser.Rows[e.RowIndex].FindControl("lbDelete") as LinkButton;
            int id = Convert.ToInt32(deleteButton.CommandArgument);
            manager.DeleteCommentById(id);

            BindDataForGridView(string.Empty);
        }

        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            BindDataForGridView(string.Empty);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            List<UserInfo> users = null;
            string searchString = txtUserName.Text.Trim();
            BindDataForGridView(searchString);
        }
    }
}